package io.renren.modules.backstage.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import io.renren.common.exception.Result;
import io.renren.modules.backstage.feign.CourseFeignClients;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.backstage.entity.CourseEntity;
import io.renren.modules.backstage.service.CourseService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;


/**
 * 课程表
 *
 * @author ian
 * @email ian.ren121@gmail.com
 * @date 2022-04-21 19:34:30
 */
@RestController
@RequestMapping("backstage/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @Resource
    private CourseFeignClients courseFeignClients;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("backstage:course:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = courseService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("backstage:course:info")
    public R info(@PathVariable("id") Long id){
        com.tencent.search.pojo.CourseEntity courseEntity = courseFeignClients.getById(id);
        CourseEntity course = JSON.parseObject(JSON.toJSONString(courseEntity), CourseEntity.class);
        return R.ok().put("course", course);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("backstage:course:save")
    @Transactional
    public R save(@RequestBody CourseEntity course){
		courseService.save(course);
        com.tencent.search.pojo.CourseEntity courseEntity = JSON.parseObject(JSON.toJSONString(course), com.tencent.search.pojo.CourseEntity.class);
        courseFeignClients.save(courseEntity);
        return R.ok();
    }

    @RequestMapping("uploadImage")
    public Result uploadImage(MultipartFile file){
        Result result = courseService.uploadImage(file);
        return result;
    }


    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("backstage:course:update")
    @Transactional
    public R update(@RequestBody CourseEntity course){
		courseService.updateById(course);
        com.tencent.search.pojo.CourseEntity courseEntity = JSON.parseObject(JSON.toJSONString(course), com.tencent.search.pojo.CourseEntity.class);
        courseFeignClients.updateById(courseEntity);
        return R.ok();
    }

    @RequestMapping("searchCourse")
    public Result searchCourse(@RequestParam Map<String,Object> paramMap){
        Result result = courseService.searchCourse(paramMap);
        return result;
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("backstage:course:delete")
    @Transactional
    public R delete(@RequestBody Long[] ids){
		courseService.removeByIds(Arrays.asList(ids));
        courseFeignClients.delete(ids);
        return R.ok();
    }

}
